// pages/pay/pay.js
var util = require('../../utils/util.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderNo: 'NULL',
        money: 0,
        ubi: 0,
        orderid: 0,
        tmpid:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            orderid: options.orderid,
            tmpid: options.tmpid
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        var oid = this.data.orderid;
        wx.request({
            url: app.globalData.miniproUrl + '/Uwork/GetOrder',
            data: {
                oid: oid
            },
            success: function(res) {
                
                that.setData({
                    orderNo: res.data.ordersn,
                    money: res.data.fee,
                    ubi: res.data.ubi
                })
            }
        })
    },
    cancel:function(){
        wx.navigateTo({
            url: '../recharge/recharge',
        })
    },
    pay: function() {
        var that=this;
        var url = app.globalData.miniproUrl + 'Api/Prepay';
        var payInfo = {
            body: 'U币兑换支付',
            total_fee: this.data.money,
            order_sn: this.data.orderNo,
            openid: wx.getStorageSync('openId')
        };
        console.log(payInfo)
        wx.request({
            url: url, //改成你自己的链接
            header: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            data:payInfo,
            method: 'POST',
            success: function(res) {
                console.log(res.data);
                console.log('调起支付');
                wx.requestPayment({
                    'timeStamp': res.data.timeStamp,
                    'nonceStr': res.data.nonceStr,
                    'package': res.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.paySign,
                    'success': function(res) {
                        if (that.data.tmpid > 0) {
                            wx.request({
                                url: app.globalData.miniproUrl + 'Api/SendTpl',
                                data: {
                                    tmpid: that.data.tmpid
                                }
                            })
                        };
                        wx.showToast({
                            title: '支付成功',
                            icon: 'success',
                            duration: 3000
                        });
                    },
                    'fail': function(res) {

                    },
                    'complete': function(res) {
                       // console.log('complete');
                        wx.navigateTo({
                            url: '../rechargedetail/rechargedetail',
                        })
                    }
                });
            },
            fail: function(res) {
                console.log(res.data)
            }
        });
    }
})